<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="调拨单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入调拨单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输明细编号" prop="transportDetailId">
        <el-input
          v-model="queryParams.transportDetailId"
          placeholder="请输入运输明细编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输单号" prop="trackingNo">
        <el-input
          v-model="queryParams.trackingNo"
          placeholder="请输入运输单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送单批次号" prop="deliveryBatchId">
        <el-input
          v-model="queryParams.deliveryBatchId"
          placeholder="请输入配送单批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="目的地" prop="destination">
        <el-input
          v-model="queryParams.destination"
          placeholder="请输入目的地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入货品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货品批次" prop="productBatch">
        <el-input
          v-model="queryParams.productBatch"
          placeholder="请输入货品批次"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入总重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="数量" prop="transportQuantity">
        <el-input
          v-model="queryParams.transportQuantity"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleArrive"
          v-hasPermi="['storage:deliveryDetail:remove']"
        >处理到货</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storage:deliveryDetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deliveryDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配送详情编号" align="center" prop="deliveryDetailId" />
      <el-table-column label="调拨单号" align="center" prop="orderId" />
      <el-table-column label="配送单批次号" align="center" prop="deliveryBatchId" />
      <el-table-column label="预计送达时间" align="center" prop="expectedDeliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedDeliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="货品名称" align="center" prop="productName" />
      <el-table-column label="货品批次" align="center" prop="productBatch" />
      <el-table-column label="总重量" align="center" prop="weight" />
      <el-table-column label="重量单位" align="center" prop="weightUnit" />
      <el-table-column label="数量" align="center" prop="transportQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:deliveryDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleArrive(scope.row)"
            v-hasPermi="['storage:deliveryDetail:remove']"
          >处理到货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import {
  listDeliveryDetail,
  getDeliveryDetail,
  delDeliveryDetail,
  addDeliveryDetail,
  updateDeliveryDetail,
  arrDeliveryDetail
} from "@/api/storage/deliveryDetail";

export default {
  name: "DeliveryDetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货品配送表格数据
      deliveryDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        transportDetailId: null,
        trackingNo: null,
        deliveryBatchId: null,
        warehouseId: null,
        destination: null,
        expectedDeliveryTime: null,
        deliveryTime: null,
        state: null,
        productName: null,
        productBatch: null,
        weight: null,
        weightUnit: null,
        transportQuantity: null,
        type: null,
        generatedDate: null,
        updatedDate: null,
        updatedPerson: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "调拨单号不能为空", trigger: "blur" }
        ],
        transportDetailId: [
          { required: true, message: "运输明细编号不能为空", trigger: "blur" }
        ],
        trackingNo: [
          { required: true, message: "运输单号不能为空", trigger: "blur" }
        ],
        deliveryBatchId: [
          { required: true, message: "配送单批次号不能为空", trigger: "blur" }
        ],
        destination: [
          { required: true, message: "目的地不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "货品名称不能为空", trigger: "blur" }
        ],
        productBatch: [
          { required: true, message: "货品批次不能为空", trigger: "blur" }
        ],
        weight: [
          { required: true, message: "总重量不能为空", trigger: "blur" }
        ],
        weightUnit: [
          { required: true, message: "重量单位不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        generatedDate: [
          { required: true, message: "生成时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询货品配送列表 */
    getList() {
      this.loading = true;
      //this.queryParams.state = '待发货'
      listDeliveryDetail(this.queryParams).then(response => {
        this.deliveryDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deliveryDetailId: null,
        orderId: null,
        transportDetailId: null,
        trackingNo: null,
        deliveryBatchId: null,
        warehouseId: null,
        destination: null,
        expectedDeliveryTime: null,
        deliveryTime: null,
        state: null,
        productName: null,
        productBatch: null,
        weight: null,
        weightUnit: null,
        transportQuantity: null,
        type: null,
        generatedDate: null,
        updatedDate: null,
        updatedPerson: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deliveryDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货品配送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deliveryDetailId = row.deliveryDetailId || this.ids
      getDeliveryDetail(deliveryDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货品配送";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deliveryDetailId != null) {
            updateDeliveryDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeliveryDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 处理到货按钮操作 */
    handleArrive(row) {
      const deliveryDetailIds = row.deliveryDetailId || this.ids;
      this.$modal.confirm('是否处理货品配送编号为"' + deliveryDetailIds + '"的数据项？').then(function() {
        return arrDeliveryDetail(deliveryDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("已添加到待入库列表");
        // //跳转到deliveryEntry页面
        // this.$router.push({ path: '/storage/deliveryEntry' });
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/deliveryDetail/export', {
        ...this.queryParams
      }, `deliveryDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
